﻿using FRMS.Model;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Hosting;
using FRMS.BUS;
using System.Data;
using System;
using System.IO;
using System.Xml;
using Microsoft.VisualStudio.Web.CodeGeneration.Contracts.Messaging;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using FRMS_N71SP.BUS;
using FRMS.DAL;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    //[Authorize]
    public class AttendanceController : ControllerBase
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public AttendanceController(IHostingEnvironment hostingEnvironment, IHttpContextAccessor access)
        {
            JwtSettings jwt = new JwtSettings();
            jwt.Audience = AppSettingsHelper.Configuration["JwtSettings:Audience"];
            jwt.Issuer = AppSettingsHelper.Configuration["JwtSettings:Issuer"];
            jwt.SecretKey = AppSettingsHelper.Configuration["JwtSettings:SecretKey"];
            _jwtSettings = jwt;
            _hostingEnvironment = hostingEnvironment;
            _access = access;
        }

        /// <summary>
        /// 导入年度非工作日
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("LoadDate")]
        public ResponseMessage LoadDate(int year, [FromForm] IFormCollection formCollection)
        {
            ResponseMessage response = new ResponseMessage();
            string webRootPath = _hostingEnvironment.ContentRootPath;
            FormFileCollection filelist = (FormFileCollection)formCollection.Files;
            foreach (IFormFile file in filelist)
            {
                response = ImportAll(webRootPath, file, year);
            }
            return response;
        }

        public ResponseMessage ImportAll(string webRootPath, IFormFile file, int year)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                AttendanceBUS attendanceBUS = new AttendanceBUS();
                DataTable data = new DataTable();
                string filepath = ExcelTools.UploadFileToServer(file, webRootPath);
                data = ExcelTools.ImportCompareMeanExcel(filepath, 0);
                message = attendanceBUS.ImportDate(webRootPath, data, year);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "一键导入";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "一键导入异常";
            }
            return message;
        }

        /// <summary>
        /// 查询考勤记录
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryAttendance")]
        public ResponseMessage QueryAttendance(CheckInOutInfo checkInOutInfo)
        {
            ResponseMessage response = new ResponseMessage();
            AttendanceBUS attendanceBUS = new AttendanceBUS();
            response = attendanceBUS.QueryRecord(checkInOutInfo);
            return response;
        }

        [HttpPost]
        [Route("ImportOwner")]
        public ResponseMessage ImportOwner([FromForm] IFormCollection formCollection)
        {
            ResponseMessage response = new ResponseMessage();
            string webRootPath = _hostingEnvironment.ContentRootPath;
            FormFileCollection filelist = (FormFileCollection)formCollection.Files;
            foreach (IFormFile file in filelist)
            {
                response = Import(webRootPath, file);
            }
            return response;
        }

        public ResponseMessage Import(string webRootPath, IFormFile file)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                AttendanceBUS attendanceBUS = new AttendanceBUS();
                DataTable data = new DataTable();
                string filepath = ExcelTools.UploadFileToServer(file, webRootPath);
                data = ExcelTools.ImportCompareMeanExcel(filepath, 0);
                message = attendanceBUS.ImportOwner(webRootPath, data);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "一键导入";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "一键导入异常";
            }
            return message;
        }

        /// <summary>
        /// 导出考勤统计表
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ExportAttendanceReport")]
        public ResponseMessage ExportAttendanceReport(string starttime, string endtime)
        {
            ResponseMessage response = new ResponseMessage();
            string sWebRootFolder = _hostingEnvironment.ContentRootPath;
            AttendanceBUS attendanceBUS = new AttendanceBUS();
            int projectid = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
            response = attendanceBUS.ExportRecord(sWebRootFolder, starttime, endtime, projectid);
            return response;
        }

        /// <summary>
        /// 同步考勤记录
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("SyncRecord")]
        public ResponseMessage SyncRecord(string starttime, string endtime)
        {
            ResponseMessage response = new ResponseMessage();
            N71SPDeviceBUS n71SPDeviceBUS = new N71SPDeviceBUS();
            response = n71SPDeviceBUS.SyncRecord(starttime, endtime);
            return response;
        }

        /// <summary>
        /// 同步人员
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("SyncPerson")]
        public ResponseMessage SyncPerson()
        {
            ResponseMessage response = new ResponseMessage();
            N71SPDeviceBUS n71SPDeviceBUS = new N71SPDeviceBUS();
            response = n71SPDeviceBUS.SyncPerson();
            return response;
        }

        [HttpPost]
        [Route("Test")]
        public ResponseMessage Test()
        {
            ResponseMessage response = new ResponseMessage();
            N71SPDeviceBUS n71SPDeviceBUS = new N71SPDeviceBUS();
            DeviceDAL deviceDAL = new DeviceDAL();
            DeviceInfo deviceInfo = deviceDAL.LoadDeviceInfo(1);
            n71SPDeviceBUS.N71SPQueryPeron(deviceInfo);
            return response;
        }

        /// <summary>
        /// 查询部门
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryDepart")]
        public ResponseMessage QueryDepart()
        {
            ResponseMessage response = new ResponseMessage();
            AttendanceBUS attendanceBUS = new AttendanceBUS();
            response = attendanceBUS.QueryDepart();
            return response;
        }

        /// <summary>
        /// 导出加班记录
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ExportWorkOverTime")]
        public ResponseMessage ExportWorkOverTime(string starttime, string endtime)
        {
            ResponseMessage response = new ResponseMessage();
            string sWebRootFolder = _hostingEnvironment.ContentRootPath;
            AttendanceBUS attendanceBUS = new AttendanceBUS();
            response = attendanceBUS.ExportWorkOverTime(sWebRootFolder, starttime, endtime);
            return response;
        }
    }
}
